<?php

/**
 * MVM_MALL 网上商店系统 团购活动
 * ============================================================================
 * 版权所有 (C) 2007-2010 www.mvmmall.com，并保留所有权利。
 * 网站地址: http://www.mvmmall.com
 * ----------------------------------------------------------------------------
 * 这是一个免费开源的软件；这意味着您可以在不用于商业目的的前提下对程序代码
 * 进行修改、使用和再发布。
 * ============================================================================
 * $Author:  www.mvmmall.com  $
 * $Date: 2008-04-12 $
 * $Id: group.php  www.mvmmall.com$
 * ---------------------------------------------
*/

require_once './include/common.inc.php';
if($action=='list'){
	$total_count = $db->counter("{$tablepre}activity","where act_type='1'");
	require_once MVMMALL_ROOT.'./include/pager.class.php';
	$page        = $page ? (int)$page : 1;
	$list_num    = $mm_limit_list;
	$rowset      = new Pager($total_count,$list_num,$page);
	$from_record = $rowset->_offset();
	$group_list  = activity(1,$from_record,$list_num);
	if ($rewrite == 1) {
		$baseurl = str_replace('1.html','',GetBaseUrl('group','list'));
		$exc='.html';
	} else{
		$baseurl = GetBaseUrl('group',$action).'&page=';
	}
	$page_list   = $rowset->link($baseurl, $exc);
	$navigation  = make_navigation($lang['navi_group'],$baseurl.$page.$exc);//导航
	require_once MVMMALL_ROOT . 'header.php';
	require_once template('group');
	footer();
} elseif ($action=='view' && is_numeric($id)){
	$mm_brand                    = $cache->get_cache('brand');
	$group                       = $db->get_one("SELECT a.*,b.uid,b.goods_file1,b.goods_name,b.goods_brand FROM `{$tablepre}activity` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.act_type='1' AND a.act_id='$id'");
	$group['photo']              = $group['goods_file1'];
	$group['url']                = GetBaseUrl('product',$group['goods_id']);
	$group['brand']              = $mm_brand[$group['goods_brand']];
	$group['title']              =  "<a href=\"$group[url]\" title=\"$group[goods_name]\">$group[goods_name]</a>";
	$act_config                  = unserialize($group['act_config']);
	$act_config['start_price']   = currency($act_config['start_price']);
	$act_config['middle_price']  = currency($act_config['middle_price']);
	$act_config['end_price']     = currency($act_config['end_price']);
	$act_config['deposit']       = currency($act_config['deposit']);
	$group['time']               = date($mm_date_format,$group['end_time']);
	if($group['is_finished']==1 || $group['end_time']<$m_now_time){
		$end_ss                  = 0;
	} else {
		$lang['activity_end']    = $lang['auction_time'];
		$end_ss                  = $group['end_time']-$m_now_time;
		$group['buy']            = "<input type=\"submit\" class=\"btn2\" name=\"Submit\" value=\"$lang[buy]\">";
	}
	$navigation                  = make_navigation($lang['navi_group'],GetBaseUrl('group','list'));//导航
	require_once MVMMALL_ROOT . 'header.php';
	require_once template('group_view');
	footer();
}elseif ($action=='buy' && is_numeric($id) && is_numeric($goods_id)){
	!$m_check_id &&  show_msg('login_please',GetBaseUrl('logging','login'));
	$group       = $db->get_one("SELECT a.*,b.uid,b.goods_file1,b.goods_name,b.uid,b.goods_sale_price FROM `{$tablepre}activity` a LEFT JOIN `{$tablepre}goods_table` b ON a.goods_id=b.uid WHERE a.act_type='1' AND a.act_id='$id'");
	if($group['is_finished']==1 || $group['end_time']<$m_now_time){
		show_msg('activity_end');
	}else{
		$act_config              = unserialize($group['act_config']);
		$buy_nums                = $act_config['buy_nums']+1; //订购人数
		//判断团购限购数量 0表示没有数量限制
		if($act_config['restric_nums']>0 && $buy_nums>$act_config['restric_nums']){
			$db->query("UPDATE `{$tablepre}activity` SET is_finished='1' WHERE act_type='1' AND act_id='$id'");//活动结束
			show_msg('activity_end');
		}
		//读会员信息
		$user = $db->get_one("SELECT member_money,member_name,member_email,member_tel1,member_tel2,member_address,member_zip FROM {$tablepre}member_table WHERE member_id = '$m_check_id'");
		$user['member_money'] < $price  &&   show_msg('money_error');
		$old_list = $db->get_one("SELECT uid,cart_id,cart_goods_id,cart_num FROM `{$tablepre}cart_table` WHERE cart_id = '$m_check_id' AND cart_type='1' AND cart_goods_id = '$goods_id'");
		if($old_list['uid']) {
			move_page('cart.php?action=buy');
		} else {
			if($group['uid']) {
				//判断库存
				!goods_stock_check($group['uid'],1) && show_msg('sale_num');
				//团购价格
				if($buy_nums>=$act_config['end_nums']){
					$price   = $act_config['end_price'];
				}elseif ($buy_nums>=$act_config['middle_nums']){
					$price   = $act_config['middle_price'];
				}else {
					$price   = $act_config['start_price'];
				}
				//end
				//写入购物车
				$add_list['goods_name']       = dhtmlchars($add_list['goods_name']);
				$query = "INSERT INTO `{$tablepre}cart_table` SET
	                      cart_wish           = '0',
					      cart_id             = '$m_check_id',
                          cart_goods_id       = '$group[uid]',
                          cart_sess           = '$m_check_cart',
                          cart_subject        = '$group[goods_name]',
                          cart_price          = '$price',
                          cart_num            = '1',
                          cart_type           = '1',
                          register_date       = '$m_now_time'
                         ";
				$db->query($query);
				$_SESSION['activity']['type'] = 1;
				$_SESSION['activity']['id']   = $group['act_id'];
				//end
				move_page('cart.php?action=buy');
			} else {
				show_msg('pass_worng');
			}
		}
	}

}else {
	show_msg('pass_worng');
}